package com.uwlaser.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DataCellCapacity;
import com.uwlaser.service.DataCellCapacityService;
import com.uwlaser.service.dto.DataCellCapacityDto;
import com.uwlaser.service.dto.DataCellCapacityQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author caoguilin
* @date 2023-02-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "电芯容量管理")
@RequestMapping("/api/dataCellCapacity")
public class DataCellCapacityController {

    private final DataCellCapacityService dataCellCapacityService;

    @GetMapping
    @Log("查询电芯容量")
    @ApiOperation("查询电芯容量")
    @PreAuthorize("@el.check('dataCellCapacity:list')")
    public ResponseEntity query(DataCellCapacityQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataCellCapacityService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增电芯容量")
    @ApiOperation("新增电芯容量")
    @PreAuthorize("@el.check('dataCellCapacity:add')")
    public ResponseEntity create(@Validated @RequestBody DataCellCapacityDto resources){
        return new ResponseEntity<>(dataCellCapacityService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改电芯容量")
    @ApiOperation("修改电芯容量")
    @PreAuthorize("@el.check('dataCellCapacity:edit')")
    public ResponseEntity update(@Validated @RequestBody DataCellCapacityDto resources){
        dataCellCapacityService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    @PostMapping("/clearCellCapacity")
    @Log("清空电芯档位数据")
    @ApiOperation("清空电芯档位数据")
    @PreAuthorize("@el.check('dataCellCapacity:clearCellCapacity')")
    public ResponseEntity deleteAll() {
        dataCellCapacityService.remove(new QueryWrapper<>());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除电芯容量")
    @ApiOperation("删除电芯容量")
    @PreAuthorize("@el.check('dataCellCapacity:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        dataCellCapacityService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    /*
    @Log("导出电芯容量")
    @ApiOperation("导出电芯容量")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataCellCapacity:list')")
    public void download(HttpServletResponse response, DataCellCapacityQueryParam query) throws IOException {
        dataCellCapacityService.download(dataCellCapacityService.queryAll(query), response);
    }*/


    @PostMapping("/importCellCapacityBatch")
    @Log("批量导入容量数据")
    @ApiOperation("批量导入容量数据")
    @PreAuthorize("@el.check('dataCellCapacity:add')")
    public ResponseEntity importCellCapacity(@Validated @RequestBody List<DataCellCapacityDto> resources){
        return new ResponseEntity<>(dataCellCapacityService.importCellCapacityBatch(resources),HttpStatus.CREATED);
    }
}
